﻿// JScript 文件

// JScript 文件
//获取分页
(function(){
PagingPanel = {
    g_PageSize:20,
    g_EventName:null,
    getPrePage : function(page){    
        return ((page-1)>0)?(page-1):1;
    },
    getNexPage : function (page,pagecount){
        return  (parseInt(page) + 1 > pagecount) ? pagecount : parseInt(page) + 1;
    },
    //检查是否录入数字
    checkTxt:function(point){   
        var str = document.getElementById(point.id).value;   
        if(!this.IsNumber(str)){
            alert("请输入大于0的整数");
            document.getElementById(point.id).value = "";
        }
        if (event.keyCode == 13)
            goToPage(parseInt(str));
    },
    IsNumber : function(str){
        var reg = new RegExp("(^[1-9]+[0-9]*$)");
        if(!reg.test(str))
            return false;
        return true;
    },
     IsNumberContainZero : function(str){
        var reg = new RegExp("(^[0-9]*$)"); //正规表达式-非负整数
        if(!reg.test(str))
            return false;
        return true;
    },
    /////通过总记录数计算总共的页数
    getPageCount:function( nTotalCount ){
        if( nTotalCount <= 0 ){
            return 0;
        }
        else{
            var tPageCount = 0;
            if( ( nTotalCount % this.g_PageSize ) == 0){
                tPageCount = nTotalCount / this.g_PageSize;
            }
            else{
                tPageCount = Math.ceil( nTotalCount / this.g_PageSize );
            } 
            return tPageCount;
        }
    },
    goToPage:function(nPage) {
       this.g_EventName(nPage); 
    },
    showPageByNum : function() {
        var str = document.getElementById("pagenum").value;
        if(!this.IsNumber(str)){
            alert("请输入大于0的整数");
            document.getElementById("pagenum").value="";
        }
        this.goToPage(parseInt(str));
    },
    getPagingHtml : function (nTotalCount,nPageIndex) {
        var tHtml = '';
        var nStartPage = Math.round(this.g_PageSize / 2 - 0.5);
        //总页数
        var cPageCount = this.getPageCount(nTotalCount);
        if( cPageCount > 1 ){
            //开始页        
            var cStartPageIndex = 1;
            //结束页
            var cEndPageIndex = cPageCount;
            var starthtm = '';
            var endhtm = '';
            if( cPageCount > this.g_PageSize ){ 
                if( ( nPageIndex - nStartPage ) > 1 )
                {  
                   cStartPageIndex = nPageIndex - nStartPage; 
                   if(  cStartPageIndex-1 >1)
                   {
                    starthtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage(1)">1</span>';
                    starthtm+='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: none;" onclick="PagingPanel.goToPage('+(nPageIndex-1)+')">...</span>';
                   }
                   else
                   {
                    starthtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage(1)">1</span>';
                   }
                }
                if( ( cStartPageIndex + this.g_PageSize - 1 ) < cPageCount )
                {   
                     cEndPageIndex = cStartPageIndex + this.g_PageSize - 1;
                      if( cPageCount-cEndPageIndex>1 )
                     { 
                        endhtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: none;" onclick="PagingPanel.goToPage('+(nPageIndex+1)+')">...</span>';
                        endhtm+='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage('+cPageCount+')">'+cPageCount+'</span>';
                     }
                     else
                     {
                        endhtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage('+cPageCount+')">'+cPageCount+'</span>';
                     }
                }
                else
                { 
                
                    if( ( cEndPageIndex - this.g_PageSize + 1 ) > 0 
                       && ( cEndPageIndex - this.g_PageSize + 1 ) < cStartPageIndex 
                       ){
                           cStartPageIndex = cEndPageIndex - this.g_PageSize + 1;
                       }
                   if(  cStartPageIndex-1 >1)
                   {
                    starthtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage(1)">1</span>';
                    starthtm+='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: none;" onclick="PagingPanel.goToPage('+(nPageIndex-1)+')">...</span>';
                   }
                   else
                   {
                    starthtm='&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage(1)">1</span>';
                   }
                   
                }
            } 
            tHtml +='<div><table style="margin:0px auto;"><tr height=22px>';
            //首页
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            tHtml +='&nbsp;<a href="javascript:void(0)" onclick="PagingPanel.goToPage(1)">首页</a>&nbsp;</td>';
            //分格
             //上一页
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            if (nPageIndex==1)
            {
                tHtml +='&nbsp;上一页&nbsp;';
            }
            else
            {
                tHtml +='&nbsp;<a href="javascript:void(0)" onclick="PagingPanel.goToPage(' + this.getPrePage(nPageIndex) + ')">上一页</a>&nbsp;';
            }
            var htm=starthtm;
             for( var cIndex = (cStartPageIndex - 1); cIndex < cEndPageIndex;cIndex++ )
             {
                if( cIndex == (nPageIndex - 1 ) ){//当前选中项
                    htm += '&nbsp;&nbsp;<span><font color="red">'
                           + ( cIndex + 1 )
                           + '</font><span>';
                }
                else
                {
                    htm += '&nbsp;&nbsp;<span style="cursor:pointer;text-decoration: underline;" onclick="PagingPanel.goToPage( '
                           + ( cIndex + 1 )
                           + ' )" >'
                           + ( cIndex + 1 )
                           + '</span>';
                }
             }
             htm+=endhtm;
            tHtml +='<td width=3px>'+htm+'</td>';
           
            //分格
            tHtml +='<td width=3px></td>';
            //下一页
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            if (nPageIndex==cPageCount)
            {
                tHtml +='&nbsp;下一页&nbsp;';
            }
            else
            {
                tHtml +='&nbsp;<a href="javascript:void(0)" onclick="PagingPanel.goToPage(' + this.getNexPage(nPageIndex,cPageCount) + ')">下一页</a>&nbsp;';
            }
            //分格
            tHtml +='<td width=3px></td>';
            //尾页
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            tHtml +='&nbsp;<a href="javascript:void(0)" onclick="PagingPanel.goToPage('+cPageCount+')">尾页</a>&nbsp;</td>';
            //分格
            tHtml +='<td width=3px></td>';
            //页码
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            tHtml +='<input onkeyup="PagingPanel.checkTxt(this)" value='+nPageIndex+' id="pagenum" type=text style="width:30px;vertical-align: middle; text-align: center;background-color:white;border-right:white 1px solid; border-top:white 1px solid; border-left:white 1px solid; border-bottom:white 1px solid;"/></td>';
            //分格
            tHtml +='<td width=3px></td>';
            //GO
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            tHtml +='&nbsp;<a href="javascript:void(0)" onclick="PagingPanel.showPageByNum();return false;">GO</a>&nbsp;</td>';
            //分格
            tHtml +='<td width=3px></td>';
            //总页
            tHtml +='<td style="vertical-align: middle; text-align: center;background-color: #f7fdff;border-right: #9cc3d9 1px solid; border-top: #9cc3d9 1px solid; border-left: #9cc3d9 1px solid; border-bottom: #9cc3d9 1px solid;">';
            tHtml +='&nbsp;&nbsp;'+nPageIndex+'/'+cPageCount+'页&nbsp;&nbsp;</td>';                        
        }
        return tHtml;  
    }
}
})()

 




